
/****************************************************************************
**
** Copyright (C) 2004-2008 VATATA.com . All rights reserved.
**
** This file is the part of the Miniutil Poject of VATATA.com.
** Vatata's miniutil library is lightweight and portable (for linux
** and Windows). The C++ Class Library included Threadpool, Dll danymic loading, 
** General socket application model and tcp server, httpserver/servlet, etc. 
** It is a pratical utility disigned for who don't want learn the Big C++ 
** Developing Platform, such as  Boost or ACE.
**
** This file may be used under the terms of the GNU General Public
** License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file.  Alternatively you may (at
** your option) use any later version of the GNU General Public
** License if such license has been publicly approved by VATATA.com.
** (or its successors, if any) and the Miniutil Project Aministrators. 
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://www.gnu.org/licenses/lgpl.html. If you are unsure which 
** license is appropriate for your use, please review the following 
** information: http://code.google.com/p/miniutil/ or contact 
** http://www.vatata.com.
**
** In addition, as a special exception, VATATA.com, as the sole
** copyright holder for Miniutil Project, grants users of VATATA
** P2P Platform the right to release and package with the related 
** libraries without the source code.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. vatata reserves all rights not expressly
** granted herein.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/



#ifndef __MINIUTIL__ADDRESS__H__
#define __MINIUTIL__ADDRESS__H__

#include <string>

#ifdef WIN32
# include <winsock2.h>
# include <windows.h>
#else
# include <string.h>
# include <unistd.h>
# include <netinet/in.h>
# include <netdb.h>
# include <sys/socket.h>
#endif

#include <stdio.h>

#include "miniutil/datatype.h"

using namespace std;

namespace miniutil{
		class ip4addr 
		{
		public:
			static const miniutil::m_uint32_t local = (127 << 24) + 1;
			static const miniutil::m_uint32_t any = 0;

			static ip4addr LOCALHOST();
			static ip4addr INETANY();
			static ip4addr MYSELF();


		protected:
			miniutil::m_uint32_t addr; //host byte sequence
            miniutil::m_uint32_t addrda; //addr data, net byte order
		public:

			ip4addr( const char * hostname );
			ip4addr( string &hostname );
			ip4addr( const string &hostname );
			inline ip4addr( miniutil::m_uint32_t _addr = 0 )
			{
				this->addr = _addr;
                addrda = htonl(addr);
			}

			inline ip4addr( const ip4addr & iaddr)
			{
				this->addr = iaddr.addr;
                addrda = htonl(addr);
			}

			inline miniutil::m_uint32_t addrData() const
			{
				return addrda;
			}

			inline string IP() const
			{
				char buf[80];
#ifdef WIN32				
				sprintf_s(buf, 80, "%u.%u.%u.%u",(addr>>24)&0xff,(addr>>16)&0xff,(addr>>8)&0xff,addr&0xff);
#else
				sprintf(buf, "%u.%u.%u.%u",(addr>>24)&0xff,(addr>>16)&0xff,(addr>>8)&0xff,addr&0xff);
#endif
                std::string rstr;
                rstr.assign(buf);
				return rstr;
			}

			inline string hostname()
			{
				struct hostent* hostEntry;
				hostEntry = gethostbyaddr( (char *) &addr, 4, AF_INET);
				  
				if (hostEntry == NULL ) 
					return IP();
				return string(hostEntry->h_name);
			}

			inline bool isLocalHost()
			{
				return ( addr == local );
			}

			inline bool isInetAny()
			{
				return 0 == addr ;
			}

			inline bool isClassA()
			{
				return ( (addr>>24) < 128 );
			}

			inline bool isClassB()
			{
				return ( (addr>>24) >= 128 ) && (( addr >> 24) < 192 );
			}

			inline bool isClassC()
			{
				return ( (addr>>24) >= 192 ) && (( addr >> 24) < 224 );
			}

			inline bool isMultiCast()
			{
				return (addr>>24)==224;
				//TODO: ĵַ
			}
			static bool isReserveIp(int ip);
			inline bool isReserveIp() const
			{
				return isReserveIp(this->addr);
			}
			inline bool isReserveIp(const ip4addr & ip) const
			{
				return isReserveIp( ip.addr/*(int) ip.addrData()*/ );
			}
		};

		class soaddr
		{
		protected:
			struct sockaddr_in saddr;
			ip4addr ip;
			int port;
		public:
			inline soaddr( const soaddr & addr ) : ip(addr.ip), port(addr.port)
			{
				memset(&saddr, 0x00, sizeof(saddr)); //todo, maybe just call get_sockaddr
			}

			inline soaddr( int _port, const ip4addr & _ipaddr ) : ip( _ipaddr ), port(_port)
			{
				memset(&saddr, 0x00, sizeof(saddr));
			}

			inline soaddr( struct sockaddr_in * sockaddr = NULL ) : port(0)
			{
				if ( sockaddr != NULL )
				{
					port = ntohs( sockaddr->sin_port );
					ip = ip4addr( ntohl(sockaddr->sin_addr.s_addr) );
				}
				memset(&saddr, 0x00, sizeof(saddr));
			}
			soaddr(const string & soaddress_string );

			inline void setIP( const ip4addr & _ip ) 
			{
				this->ip = _ip ;
			}

			inline void setPort( int _port )
			{
				this->port = _port;
			}

			inline int getPort() const
			{
				return port;
			}
			inline const ip4addr & getIP() const
			{
				return ip ;
			}
			inline string getIPAddress() const
			{
                return ip.IP();
			}

			inline struct sockaddr * get_sockaddr()
			{
				memset( &saddr, 0, sizeof(struct sockaddr_in) );
				saddr.sin_family = AF_INET;
				saddr.sin_addr.s_addr   =   ip.addrData();
				saddr.sin_port   = htons( (miniutil::m_uint16_t)port );

				return (sockaddr *)&saddr ;
			}

		};


	inline bool operator== (const ip4addr &ip1, const ip4addr &ip2 )
	{
		return ip1.addrData() == ip2.addrData();
	}

	inline bool operator!= (const ip4addr &ip1, const ip4addr &ip2 )
	{
		return ip1.addrData() != ip2.addrData();
	}

	inline bool operator<(const ip4addr &ip1, const ip4addr &ip2 )
	{
		return ip1.addrData() < ip2.addrData();
	}


	inline bool operator== (const soaddr &sa1, const soaddr & sa2 )
	{
		return ( sa1.getIP() == sa2.getIP() ) && ( sa1.getPort() == sa2.getPort() );
	}

	inline bool operator!= (const soaddr &sa1, const soaddr & sa2 )
	{
		return ( sa1.getIP() != sa2.getIP() ) || ( sa1.getPort() != sa2.getPort() );
	}

	inline bool operator< (const soaddr &sa1, const soaddr & sa2 )
	{
		if ( sa1.getIP() == sa2.getIP() )
			return sa1.getPort() < sa2.getPort();
		return ( sa1.getIP() < sa2.getIP() );
	}


}

using namespace miniutil;


#endif
